package com.ficus.road.maintain.third.zh;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ficus.road.maintain.core.model.RoadInfo;
import com.ficus.road.maintain.core.model.RoadTypeEnum;
import com.ficus.road.maintain.core.model.smart.District;
import com.ficus.road.maintain.dao.RoadInfoDao;
import com.ficus.road.maintain.dao.smart.DistrictDao;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiawen yang
 * @date 2022/4/15 下午8:49
 */
@Component
public class ZHDistrictPCIAndPQI extends BaseZH {

    @Autowired
    private DistrictDao districtDao;
    @Autowired
    private RoadInfoDao roadInfoDao;

    @PostConstruct
    void init() {
        arg0 = "ytkjGetTownAvgRqiPqi";
        //农村公路:NCGL 直管公路:ZGGL 城市道路:CSDL
        arg1 = "{'year':'2022','code':'NCGL'}";
        errMsg = "调用中海接口获取PCI和PQI异常!";
        successMsg = "调用中海接口获取PCI和PQI成功!";
    }

    //    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "0 0 14 10 * ?", zone = "GMT+8:00")
    public void refreshPCIAndPQI() {
        init();
        String districtPciPqi = getZHData();
        if (ObjectUtils.isNotEmpty(districtPciPqi)) {
            JSONArray jsonArray = JSON.parseArray(districtPciPqi);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String towncode = jsonObject.getString("towncode");
                String pci = jsonObject.getString("pci");
                String pqi = jsonObject.getString("pqi");
                if (StringUtils.isNotBlank(towncode)) {
                    District fetch = districtDao.fetch(towncode);
                    if (fetch == null) {
                        fetch = new District();
                        fetch.setId(towncode);
                        String town = jsonObject.getString("town");
                        fetch.setName(town);
                    }
                    fetch.setPci(pci);
                    fetch.setPqi(pqi);
                    fetch.setTotalScore(pqi);
                    districtDao.saveOrUpdate(fetch);
                }
            }
        }
        List<RoadInfo> roadInfoList = roadInfoDao.findAll();
        Map<Pair<String, RoadTypeEnum>, Double> districtLengthMap = new HashMap<>();
        for (RoadInfo roadInfo : roadInfoList) {
            String districtId = roadInfo.getDistrictId();
            RoadTypeEnum roadType = roadInfo.getRoadType();
            Pair<String, RoadTypeEnum> key = Pair.of(districtId, roadType);
            Double tmpLength = districtLengthMap.getOrDefault(key, 0d);
            String secLen = roadInfo.getSecLen();
            if (NumberUtils.isCreatable(secLen)) {
                tmpLength += Double.valueOf(secLen);
                districtLengthMap.put(key, tmpLength);
            }
        }
        DecimalFormat df = new DecimalFormat("#.00");
        districtLengthMap.forEach((k, v) -> {
            String key = k.getKey();
            RoadTypeEnum roadType = k.getRight();
            if (StringUtils.isNotBlank(key)) {
                District fetch = districtDao.fetch(key);
                String lengthString = df.format(v);
                Double length = Double.valueOf(lengthString);
                if (fetch != null) {
                    if (roadType.equals(RoadTypeEnum.ROAD)) {
                        fetch.setRoadLength(length);
                    } else if (roadType.equals(RoadTypeEnum.DIRECT)) {
                        fetch.setDirectLength(length);
                    } else if (roadType.equals(RoadTypeEnum.HIGHWAY)) {
                        fetch.setHighwayLength(length);
                    }
                    districtDao.saveOrUpdate(fetch);
                }
            }
        });
    }
}
